package cn.jinjimi.app.main.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.CompoundButton;

import cn.jinjimi.app.R;

/**
 * Created by lforxeverc on 16/8/31.
 */
public class ToggleButton extends CompoundButton {
    private Paint togglePaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    private Bitmap bmCheck, bmUncheck;

    public ToggleButton(Context context) {
        this(context, null);
    }

    public ToggleButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ToggleButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.setClickable(true);
        bmCheck = BitmapFactory.decodeResource(getResources(), R.drawable.ico_navbar_invisible);
        bmUncheck = BitmapFactory.decodeResource(getResources(), R.drawable.ico_navbar_visible);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Bitmap bm = isChecked() ? bmCheck : bmUncheck;
        canvas.drawBitmap(bm, (getWidth() - bm.getWidth()) / 2, (getHeight() - bm.getHeight()) / 2, togglePaint);
    }

    @Override
    public void setChecked(boolean checked) {
        super.setChecked(checked);
        invalidate();
    }
}
